Semicolon and | | and | in linux

  • 2020-09-16 07:54:17
  • OfStack

With the linux command, we can execute multiple commands on one line or conditionally execute the next command. Let's look at the linux semicolon & & and & , | and |, |

";" The semicolon usage

Method: command1; command2

Use; Each command is separated by the number, and each command is executed from left to right in order, regardless of whether it fails or not. All commands are executed.

Use of the "|" pipe character

The output of the previous command as an argument to the next command

Method: command1 | command2

The pipe character "|" provided by Linux separates the two commands, and the output of the command on the left of the pipe serves as input to the command on the right of the pipe. Continuous use of pipes means that the output of the first command is used as input to the second command, the output of the second command is used as input to the third command, and so on

Use 1 pipe
# rpm -qa|grep licq
This command creates a pipe using the 1 pipe character "|". The pipe lists the RPM packages with the licq character by taking the output of the ES31en-ES32en command (including all installed RPM packages on the system) as input to the grep command.

Using multiple pipes
# cat /etc/passwd | grep /bin/bash | wc -l
This command USES two pipes. The first pipe sends the output of the cat command (showing the contents of the passwd file) to the grep command. The grep command finds all lines containing "/bin /bash". The second pipe sends the output of grep to the wc command, which counts the number of lines in the input. The function of this command is to find out how many users are on the system using bash

" & Symbol usage

& Following the start parameter means to set the process as a background process

Method: command1 &

By default, the process is the foreground process, so Shell is taken over, we can't do anything else, for processes that are not interacting, a lot of times, we want to start it in the background, we can add 1 'to the start parameter. & 'To achieve this end.

" & & Symbol usage

When shell executes a command, it returns a return value stored in the shell variable $?. In the. When $& # 63; == 0, indicates successful execution; When $& # 63; == 1 (which I think is a non-zero number and returns a value between 0 and 255) indicates a failed execution.

Sometimes, the next command depends on whether the previous command executed successfully. For example, execute another command after successfully executing one command, or execute another command after failing to execute one command, etc. shell provides & & And || to achieve command execution control function, shell will be based on & & Or the return value of the previous command || to control the execution of subsequent commands.

The syntax format is as follows:

command1 & & command2 [ & & command3 ...]

Use between commands & & Connect, realize the function of logic and.
Only in the & & The command on the left returns true (the command returns $? = = 0), & & The command on the right will be executed.
As long as 1 command returns false (command return value $? == 1), the following command will not be executed.

"||" symbol usage

Logic or function

The syntax format is as follows:

command1 || command2 [|| command3 ...]

Use || connections between commands to achieve logic or function.
Only the command to the left of || returns false (command return value $? == 1), the command to the right of || will be executed. This is the same logic or syntax as in c, which implements short-circuit logic or operations.
As long as 1 command returns true (command return value $? == 0), the following command will not be executed. The execution stops until it returns to the real place.

For example, the ping command determines a live host

ping -c 1 -w 1 192.168.1.1 & > /dev/null & & result=0 ||result=1
if [ "$result" == 0 ];then
echo "192.168.1.1 is UP!"
else
echo "192.168.2.1 is DOWN!"
fi
Pay attention to & > Write it all together.


Related articles: